// var vendors_ = require('./chunk-vendors');
var vendors_ = require('./chunk-vvv');
var axios = require('axios');
var fs = require('fs');

let myObject = {};
let name = []
function c(e) {
  if (!name.includes(e))
    name.push(e)
  if (myObject[e])
    return myObject[e].exports;
  var a = myObject[e] = {
    i: e,
    l: !1,
    exports: {}
  };
  return vendors_[e].call(a.exports, a, a.exports, c),
    a.l = !0,
    a.exports
}
let i = e82a = c("e82a")
console.log('name', name)

module.exports = { e82a };

function responseHandle_a(t) {
  var e = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "zg35ws76swnxz679"
    , s = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : "z66qa18l0w9o521k"
    , a = i["enc"].Utf8.parse(e)
    , n = i["enc"].Utf8.parse(s)
    , o = i["AES"].encrypt(t, a, {
      iv: n,
      mode: i["mode"].CBC,
      padding: i["pad"].Pkcs7
    });
  return o.toString()
}

function responseHandle_b(t) {
  var e = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "zg35ws76swnxz679"
    , s = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : "z66qa18l0w9o521k"
    , a = i["enc"].Utf8.parse(e)
    , n = i["enc"].Utf8.parse(s)
    , o = i["AES"].decrypt(t, a, {
      iv: n,
      mode: i["mode"].CBC,
      padding: i["pad"].Pkcs7
    });
  return o.toString(i["enc"].Utf8)
}


function savaConfig(JSESSIONID='', token='') {
  let str = `module.exports = { 
    "JSESSIONID": "${JSESSIONID}",
    "token": "${token}"
   };`
  fs.writeFile(__dirname + "/chunk-config.js", str, function (error) {
    if (error) {
      console.error("write error: " + error.message);
    } else {
      console.log("Successful Write to ");
    }
  });
}

// =======================================================
// =======================================================
// =======================================================

// get_JSESSIONID()
async function get_JSESSIONID(isLog = true) {
  /* 1/ 获取 JSESSIONID
  2/ 获取验证码。不过这里不用显示出来，下面登录的时候，会循环所有可能的验证码。
  */
  let result = await axios({
    method: 'get',
    url: 'https://pdc.capub.cn/api/common/captchaImage',
    headers: {
      "Sec-Fetch-Dest": " empty",
      "Connection": "keep-alive",
      "Referer": 'https://pdc.capub.cn/login.html',
      "sec-ch-ua": '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
      "Sec-Fetch-Mode": " cors",
      "Sec-Fetch-Site": " same-origin",
      "Accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
      "Accept-Encoding": "gzip, deflate, br",
      "Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7",
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
    },
  }).then(function (response) {
    let JSESSIONID = response.headers["set-cookie"];
    for (var a = JSESSIONID[0].split(";"), b = 0; b < a.length; b++) {
      var d = a[b].split("=");
      if (d[0] == "JSESSIONID") return d[1]
    }
  })
  isLog && console.log('JSESSIONID = ', result)
  return result
}

function setConfig(captcha) {
  let portData = {
    "loginName": "18820651349",
    "password": "zz19961120",
    "captcha": String(captcha),
    "isKeepLogin": 0
  }
  return responseHandle_a(JSON.stringify(portData));
}

async function login(JSESSIONID = "") {
  let total = 35, flag = true
  for (let index = 0; index < total; index++) {
    let result = await axios({
      method: 'POST',
      url: 'https://pdc.capub.cn/api/login',
      data: setConfig(index),
      headers: {
        "Cookie": "JSESSIONID=" + JSESSIONID,
        "Content-Type": "text/plain",
        "Sec-Fetch-Dest": " empty",
        "Connection": "keep-alive",
        "Referer": 'https://pdc.capub.cn/login.html',
        "sec-ch-ua": '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
        "Sec-Fetch-Mode": " cors",
        "Sec-Fetch-Site": " same-origin",
        "Accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
      },
    }).then(function (response) {
      console.log(index + "  " + JSON.stringify(response.data));
      if (response.data && response.data.status == "success") {
        flag = false
        return response.data || false
      }
    }).catch(function (error) {
      console.log(error);
    });
    if (flag && total - 1 == index) {
      let JSESSIONID = await get_JSESSIONID();
      login(JSESSIONID)
    }
    if (result) {
      result = JSON.parse(responseHandle_b(result.result))
      console.log('result ==================== ', result)
      console.log('token === ', result.token)
      savaConfig(JSESSIONID, result.token)
      return result;
    }
  }
}

async function main() {
  let JSESSIONID = await get_JSESSIONID();
  login(JSESSIONID)
}
main()
